import * as actions from '../actions/index.js'

const initState = [{
        id: 1,
		cls:"hide",
		maxId:2,
		movingId:0, //正在拖拽的工具ID，默认为0，工具栏为1，已放置的模块为当前模块id
		atIndex:0,  //放置在目标模块的ID，放置过后才会出现。
		showIcon:false, //
		settingId:0, //当前正在设置数据的模块ID
		loginError:false,
		creating:false,
		error:false,
		message:"",
      }, {
        id: 2,
		cls:"module-placeholder",
		showIcon:false
      }]
export default function todoMVC(state=initState,action){
  switch (action.type) {
    case actions.SET_PAGE_ID:
	  state[0].pageId=action.pageId;
	  return [...state];
      break;
    case actions.INIT_LIST:
        return state;
      break;
    case actions.DELETE_ITEM:
		//2种模式，真删除，假删除。isDelete为假删除，目前版本不使用。
		
	  state[0].loginError=false;
	  state[0].message="";
	  state[0].error=false;
	  state[0].creating=false;
        return [
          ...state.slice(0, action.id),
        // Object.assign({},state[action.id],{
          // isDelete:true
        // }),
          ...state.slice(action.id + 1)
        ];
      break;
    case actions.ADD_ITEM:
	  state[0].maxId+=1;
	  state[0].settingId = state[0].maxId;
	  state[0].loginError=false;
	  state[0].message="";
	  state[0].error=false;
	  state[0].creating=false;
	  let newCard = 
        {
          name:action.obj.name,
          mix:action.obj.mix,
		  preview:action.obj.preview,
		  showIcon:false,
		  id:state[0].settingId,
		  formField:action.obj.formField,
		  formData:action.obj.formData,
		  code:action.obj.code,
		  compId:action.obj.id,
		  isDelete:action.obj.isDelete,
		  isPrivate:action.obj.model_private,
		  ordinal:parseInt(action.obj.ordinal)
        };
	  let s = state.filter(c => c.id === 1)[0];
	  if(s.atIndex>0){
		  //两个插件交换位置
		state.splice(s.atIndex,0,newCard);
	  }else{
		  //新增插件
		state = [ ...state , newCard ];
	  }
	  
	  //版权提示放到最后
	  // 20170916：本版暂不展示版权提示
			  // {
				// id: 10000,
				// cls:"module-placeholder",
				// showIcon:false,
				// preview:<div style={{textAlign:"center",backgroundColor:"#FFFFFF"}}>本活动由微折提供技术支持</div>
			  // }
				// let last = state.filter(c => c.id === 10000)[0];
				// let sIndex = state.indexOf(last);
				// state.splice(sIndex,1);
				// state.splice(state.length,0,last);
	  //版权提示放到最后
	  
      return [...state];
      break;
    case actions.MODIFY_ITEM:
	  state[0].loginError=false;
	  state[0].message="";
	  state[0].error=false;
	  state[0].creating=false;
      return [
        ...state.slice(0, action.id),
        Object.assign({},state[action.id],{
          formData:action.valueObj
        }),
        ...state.slice(action.id + 1)
      ];
      break;
    case actions.TOGGLE_ICON:
	  state[0].settingId = action.obj.id;
	  for(var i=0;i<state.length;i++){
		  state[i]["showIcon"]=false;
	  }
      return [
        ...state.slice(0, action.id),
        Object.assign({},state[action.id],{
          showIcon:!state[action.id]['showIcon']
        }),
        ...state.slice(action.id + 1)
      ];
      break;
    case actions.MOVE_ITEM:
	  if(action.cardId>1){
		action.cardId = action.cardId==0?state.length:action.cardId;
		let card = state.filter(c => c.id === action.cardId)[0];
		let sIndex = state.indexOf(card);
		state.splice(sIndex,1);
		state.splice(action.atIndex,0,card);
      }
      return [...state];
      break;
	case actions.MOVING:
	  state[0].movingId=action.id;
	  return state;
	  break;
	case actions.OVERING:
	  state[0].atIndex=action.atIndex;
	  return state;
	  break;
	case actions.INIT_FORM:
	  state[0].formData=action.form;
	  return state;
	  break;
    case actions.LOAD_START:
	  state[0].loginError=false;
	  state[0].message="";
	  state[0].error=false;
	  state[0].creating=true;
	  return [...state];
	  break;
    case actions.LOAD_ERR:
	  state[0].loginError=action.loginFalse;
	  state[0].message=action.message;
	  state[0].error=true;
	  state[0].creating=false;
	  return [...state];
	  break;
    case actions.CLOSE_DIALOG:
	  state[0].loginError=false;
	  state[0].message=false;
	  state[0].error=false;
	  state[0].creating=false;
	  return [...state];
	  break;
    default:
      return state;
  }
}
